/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.module.login.presenter

import com.orhanobut.logger.Logger
import top.stime21.myneighbours.api.LocalDataManager
import top.stime21.myneighbours.base.MvpBasePrensenter
import top.stime21.myneighbours.data.entity.User
import top.stime21.myneighbours.module.login.NeedContract
import top.stime21.myneighbours.module.login.model.NeedModelImpl

/**
 * Author: czmc  Email: @tuwan21.com
 * Version:	1.0  Date:3/3/2018
 */
class NeedPrensenterImpl(override val view: NeedContract.View) : MvpBasePrensenter<NeedContract.View, NeedContract.Model>(), NeedContract.Presenter {
    override val model: NeedContract.Model
        get() = NeedModelImpl()
    val user: User
        get() = LocalDataManager.ins().getUser(view.context())!!
    private var page: Int = 1
    private var limit: Int = 10
    private var isLoadOver = false;

    override fun myneedMore(type: Int) {
        if(isLoadOver) return
        if (user == null)
            view.onLoginTimeOut()
        addSubscription(model.myneed(user.userId, type, page, limit).subscribe({ r ->
            if (r.isSuccess) {
                if(r.data!=null){
                    if(  r.data!!.size>=limit){
                        page++
                    }else{
                        isLoadOver = true
                    }
                }
                view.onMyNeedMoreGetSuccess(r.data!!)
            } else {
                view.showToast("获取失败")
            }
        }, { t ->
            Logger.i(t.toString())
            view.showToast("获取失败")
        }))
    }

    override fun details(id: Long) {
        view.showViewLoadding()
        addSubscription(model.details(id).subscribe({ r ->
            if (r.isSuccess) {
                if(r.data!=null){
                    view.showContent()
                    view.onDetailsGetSuccess(r.data)
                }else{
                    view.showEmpty()
                }
            } else {
                view.showError()
            }
        }, { t ->
            Logger.i(t.toString())
            view.showToast("获取详情失败")
            view.showError()
        }))

    }

    override fun myneed(type: Int) {
        view.showViewLoadding()
        page = 1
        isLoadOver = false
        if (user == null)
            view.onLoginTimeOut()
        addSubscription(model.myneed(user.userId, type, page, limit).subscribe({ r ->
            if (r.isSuccess) {
                if(r.data==null || r.data!!.size==0){
                    view.showEmpty()
                }else{
                    view.showContent()
                    view.onMyNeedGetSuccess(r.data!!)
                }
                page++
            } else {
                view.showError()
            }
            view.hideLodding()
        }, { t ->
            Logger.i(t.toString())
            view.showError()
        }))
    }

    override fun finish(id: Long) {
        if (user == null) {
            view.onLoginTimeOut()
            return
        }
        view.showLoadding()
        addSubscription(model.finish(id, user.userId).subscribe({ r ->
            if (r.isSuccess) {
                view.onFinishSuccess()
            } else {
                view.showToast("完成失败")
            }
            view.hideLodding()
        }, { t ->
            view.hideLodding()
            Logger.i(t.toString())
            view.showToast("完成失败")
        }))
    }

    override fun accept(id: Long) {
        if (user == null) {
            view.onLoginTimeOut()
            return
        }
        view.showLoadding()
        addSubscription(model.accept(id, user.userId).subscribe({ r ->
            if (r.isSuccess) {
                view.onAcceptSuccess()
            } else {
                view.showToast("接受失败")
            }
            view.hideLodding()
        }, { t ->
            view.hideLodding()
            Logger.i(t.toString())
            view.showToast("接受失败")
        }))
    }

    override fun cancel(id: Long) {
        if (user == null) {
            view.onLoginTimeOut()
            return
        }
        view.showLoadding()
        addSubscription(model.cancel(id, user.userId, 4).subscribe({ r ->
            if (r.isSuccess) {
                view.onCancleSuccess()
            } else {
                view.showToast("取消失败")
            }
            view.hideLodding()
        }, { t ->
            view.hideLodding()
            Logger.i(t.toString())
            view.showToast("取消失败")
        }))
    }

    override fun nearby(lat: Double, lon: Double) {
        addSubscription(model.nearby(lat, lon, 20).subscribe({ r ->
            if (r.isSuccess) {
                view.onNearbySuccess(r.data!!)
            } else {
                view.showToast("获取附近信息失败")
            }
        }, { t ->
            Logger.i(t.toString())
            view.showToast("获取附近信息失败")
        }))
    }

    override fun search(key: String) {
        view.showViewLoadding()
        addSubscription(model.search(key).subscribe({ r ->
            if (r.isSuccess) {
                if(r.data==null || r.data!!.size==0){
                    view.showEmpty()
                }else {
                    view.showContent()
                    view.onSearchSuccess(r.data!!)
                }
            } else {
                view.showError()
            }
        }, { t ->
            view.showError()
            Logger.i(t.toString())
        }))
    }

    override fun publish(type: Int, address: String, lat: Double, lon: Double, content: String, title: String) {
        if (user == null) {
            view.onLoginTimeOut()
            return
        }
        view.showLoadding()
        addSubscription(model.publish(type, user.userId, address, lat, lon, content, title).subscribe({ r ->
            if (r.isSuccess) {
                view.onPublishSuccess()
            } else {
                view.showToast("发布失败")
            }
            view.hideLodding()
        }, { t ->
            Logger.i(t.toString())
            view.showToast("发布失败")
            view.hideLodding()
        }))
    }


}